package com.zhoujinlong.utils;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by fangxiao on 15/12/28.
 */
public class BaseUtil {
    //一天的秒数
    public static final long ONE_DAY_TIME = 24 * 60 * 60;


    //获取Gson单例
    public volatile static Gson gson;

    public static Gson getGsonInstance() {
        if (gson == null) {
            synchronized (Gson.class) {
                if (gson == null) {
                    gson = new Gson();
                }
            }
        }
        return gson;
    }

    /**
     * 获取屏幕分辨率
     *
     * @param context
     * @return
     */
    public static int getWidth(Activity context) {
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;
        return screenWidth;
    }

    public static int getHeight(Activity context) {
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenHeigt = dm.heightPixels;
        return screenHeigt;
    }

    /**
     * 判断链表是否为空
     *
     * @param list
     * @return true:为Null或者size为0
     */
    public static boolean isEmpty(List<?> list) {
        return list == null || list.size() == 0;
    }


    /**
     * 判断链表第某个位置是否为空
     *
     * @param list
     * @param index
     * @return true:为Null或者size为0
     */
    public static boolean isEmpty(List<?> list, int index) {
        return list == null || list.size() == 0 || list.size() <= index;
    }


    /**
     * 获取年月日时间
     */
    public static String getNYYMMDD() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String yymmddString = sdf.format(new Date());
        return yymmddString;
    }


    /**
     * 获取年月日时间
     */
    public static String getNYYMMDD(int day) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        long now = date.getTime();
        long next = now + day * ONE_DAY_TIME * 1000;
        String yymmddString = sdf.format(next);
        return yymmddString;
    }


    public static int getDrawableId(Context context, String name) {
        return context.getResources().getIdentifier(name, "drawable", context.getPackageName());
    }

    public static String getResourceName(int type) {
        return "exercise_" + type;
    }

    //获取本地语言
    public static String getCountryLangue() {
        return Locale.getDefault().getLanguage();
    }

    /**
     * dp转px
     *
     * @param context
     * @param dpVal
     * @return
     */
    public static int dp2px(Context context, float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, context.getResources().getDisplayMetrics());
    }

    public static String getFileName(String fileName){
        String langugue = getCountryLangue();
        if("th".equals(langugue)||"tr".equals(langugue)){
            fileName = fileName + "_th.json";
        }else{
            fileName = fileName + ".json";
        }
        return fileName;
    }

}
